﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Voucher;

namespace DataObjects.ADO.SqlServer.Voucher
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerFixedAssetVoucherDao : IFixedAssetVoucherDao
    {


        public FixedAssetVoucher GetFixedAssetVoucher(long refId)
        {
            const string sql = @"uspGet_FixedAssetVoucher_ByID";
            object[] parms = { "@FixedAssetVoucherID", refId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAssetVoucher> GetPaged(long unitId, long refTypeId, int currentPage)
        {
            const string sql = @"uspGetPaged_ItemTransaction";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(long unitId, long refTypeId)
        {
            const string sql = @"uspGetPageSize_ItemTransaction";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        string IFixedAssetVoucherDao.InsertFixedAssetVoucher(FixedAssetVoucher fixedAssetVoucher)
        {
            const string sql = "uspInsert_FixedAssetVoucher";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(fixedAssetVoucher));
        }

        public string UpdateFixedAssetVoucher(FixedAssetVoucher fixedAssetVoucher)
        {
            const string sql = "uspUpdate_FixedAssetVoucher";
            return Db.Update(sql, CommandType.StoredProcedure, Take(fixedAssetVoucher));
        }

        public string DeleteFixedAssetVoucher(FixedAssetVoucher fixedAssetVoucher)
        {
            const string sql = @"uspDelete_FixedAssetVoucher";
            object[] parms = { "@FixedAssetVoucherID", fixedAssetVoucher.RefId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }

        public long GetSequence()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, FixedAssetVoucher> Make = reader =>
            new FixedAssetVoucher
            {
                RefId = reader["RefID"].AsLong(),
                RefTypeId = reader["RefTypeID"].AsInt(),
                RefNo = reader["RefNo"].AsString(),
                RefDate = reader["RefDate"].AsDateTime(),
                PostedDate = reader["PostedDate"].AsDateTime(),
                CurrencyCode = reader["CurrencyCode"].AsString(),
                AccountingObjectType = reader["AccountingObjectType"].AsShort(),
                CustomerId = reader["CustomerID"].AsLong(),
                EmployeeId = reader["EmployeeID"].AsLong(),
                VendorId = reader["VendorID"].AsLong(),
                AccountingObjectId = reader["AccountingObjectID"].AsLong(),
                TotalAmount = reader["TotalAmountOC"].AsDecimal(),
                JournalMemo = reader["JournalMemo"].AsString(),
                UnitId = reader["UnitID"].AsLong()
            };

        /// <summary>
        /// Takes the specified itemTransaction.
        /// </summary>
        /// <returns></returns>
        private object[] Take(FixedAssetVoucher fixedAssetVoucher)
        {
            return new object[]  
            {
                @"RefId", fixedAssetVoucher.RefId,
                @"RefTypeId", fixedAssetVoucher.RefTypeId,
                @"RefNo", fixedAssetVoucher.RefNo,
                @"RefDate", fixedAssetVoucher.RefDate,
                @"PostedDate", fixedAssetVoucher.PostedDate,
                @"CurrencyCode", fixedAssetVoucher.CurrencyCode,
                @"AccountingObjectType", fixedAssetVoucher.AccountingObjectType,
                @"CustomerID", fixedAssetVoucher.CustomerId,
                @"VendorID", fixedAssetVoucher.VendorId,
                @"AccountingObjectID",fixedAssetVoucher.AccountingObjectId,
                @"EmployeeID",fixedAssetVoucher.EmployeeId,
                @"TotalAmount",fixedAssetVoucher.TotalAmount,
                @"JournalMemo", fixedAssetVoucher.JournalMemo,
                @"UnitID", fixedAssetVoucher.UnitId
            };
        }

    }
}
